package Box_List;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JList;

public class JComBoxDemo {

	private JFrame frame;
	String motorcycle = "";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JComBoxDemo window = new JComBoxDemo();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public JComBoxDemo() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		String[] arr = { "A13", "A15", "B31", "Z56" };
		String[] months = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
		JList list = new JList(months);
		list.setBounds(24, 10, 82, 190);
		final JComboBox comboBox = new JComboBox(arr);
		comboBox.setEditable(true);
			comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!motorcycle.contains( comboBox.getSelectedItem() + ";")){
					motorcycle += comboBox.getSelectedItem() + ";";
				}
				comboBox.setSelectedItem(motorcycle);
			}
		});
		comboBox.setBounds(157, 94, 190, 45);
		frame.getContentPane().add(comboBox);
		JButton btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String temp = (String) comboBox.getSelectedItem();
				if (temp.endsWith(";")) {
					temp = temp.substring(0, temp.lastIndexOf(";"));
				}
				System.out.println(temp);
			}
		});
		btnNewButton.setBounds(241, 164, 106, 32);
		frame.getContentPane().add(btnNewButton);
		//frame.getContentPane().add(list);
	}
}
